package cn.wikiflyer.beikang.act.product;

import java.lang.reflect.Type;
import java.util.ArrayList;

import net.tsz.afinal.annotation.view.ViewInject;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.Html;
import android.text.Spanned;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.TextView;
import cn.wikiflyer.beikang.BaseActivity;
import cn.wikiflyer.beikang.IApplication;
import cn.wikiflyer.beikang.R;
import cn.wikiflyer.beikang.act.TabAct;
import cn.wikiflyer.beikang.act.home.NewsAct;
import cn.wikiflyer.beikang.act.user.setting.UserSettingLoginAct;
import cn.wikiflyer.beikang.bean.base.BKBaseBean;
import cn.wk.libs4a.WKApplication;
import cn.wk.libs4a.net.WKAsyncTaskPro;
import cn.wk.libs4a.utils.WKUmShareUtils;
import cn.wk.libs4a.view.adview.ADViewBean;
import cn.wk.libs4a.view.adview.ADViewUtils;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.itotem.imageloader.core.assist.FailReason;
import com.itotem.imageloader.core.assist.ImageLoadingListener;
import com.umeng.socialize.media.UMImage;

public class ProductInfoAct extends BaseActivity {

	private ADViewUtils adView;
	private String id;

	@ViewInject(id = R.id.info)
	private TextView info;

	@ViewInject(id = R.id.bottom_img_long)
	private ImageView bottom_img_long;

	@ViewInject(id = R.id.view_share, click = "onClick")
	private View view_share;

	private boolean isLogin;

	public void onClick(View view) {
		/**
		 * 分享
		 */
		if (view.getId() == R.id.view_share) {
			app().shareFenxiang(this);
		}
	}

	@Override
	public void loadXml() {
		setContentView(R.layout.bk_act_product_info);
	}

	@Override
	protected void onResume() {
		super.onResume();
		isLogin = (app().getLoginSession() != null);
	}

	@Override
	public void init() {
		adView = new ADViewUtils(this, findViewById(R.id.ad_view), false);
		id = getIntent().getStringExtra("id");
		id = "1";

		isLogin = (app().getLoginSession() != null);
		getHeader().setRightTxt("购买");
		if (IApplication.isOnlyOneProduct) {
		} else {
			getHeader().setLeftAsBack();
		}
		getHeader().setClickListener(null, null, new OnClickListener() {
			public void onClick(View v) {
				if (isLogin) {
					buy();
				} else {
					IApplication.showToast("请您先登录");
					startActivity(new Intent(ctx, UserSettingLoginAct.class));
				}
			}
		});
	}

	private void buy() {
		new AddProductTask(this);
	}

	@Override
	public void setData() {
		// initADData();
		new GetInfo(this);
	}

	/**
	 * 更新UI2
	 */
	private void updateUI2(ProductBean bean) {
		// 顶部广告
		ArrayList<ADViewBean> adList = new ArrayList<ADViewBean>();
		for (int i = 0; i < bean.goods_imgs.size(); i++) {
			adList.add(new ADViewBean(bean.goods_imgs.get(i).url, ""));
		}
		adView.setData(adList);
		// 中间的描述
		info.setText("" + bean.description);
	}

	/**
	 * 更新UI
	 */
	private void updateUI(ProductBean bean) {
		// 顶部广告
		ArrayList<ADViewBean> adList = new ArrayList<ADViewBean>();
		for (int i = 0; i < bean.goods_imgs.size(); i++) {
			adList.add(new ADViewBean(bean.goods_imgs.get(i).url, ""));
		}
		adView.setData(adList);
		// 中间的描述
		info.setText("" + bean.description);

		// 是否是一张大图
		if (bean.app_content != null && !bean.app_content.equals("")) {
			bottom_img_long.setVisibility(View.VISIBLE);

			// 一张大图
			app().imageLoader.displayImage(bean.app_content, bottom_img_long,
					new ImageLoadingListener() {
						public void onLoadingStarted() {
						}

						public void onLoadingFailed(FailReason arg0) {
						}

						public void onLoadingComplete(Bitmap arg0) {
							resizeView(bottom_img_long, arg0);
						}

						public void onLoadingCancelled() {
						}
					});
			return;
		} else {
			bottom_img_long.setVisibility(View.GONE);
		}

		// 药方
		// bean.parseSelf();
		// int len = bean.herbsList.length;
		// itemlist.removeAllViews();
		// for (int i = 0; i < len; i++) {
		// String[] temp = bean.herbsList[i].split("\u002c");
		// View v = View
		// .inflate(this, R.layout.bk_act_product_info_item, null);
		// TextView t1 = (TextView) v.findViewById(R.id.name);
		// TextView t2 = (TextView) v.findViewById(R.id.info);
		// ImageView img = (ImageView) v.findViewById(R.id.img);
		// t1.setText(enter(temp[0]));
		// t2.setText(Html.fromHtml(temp[1].replace("n", "<br>").replace("\\",
		// "")));
		// app().imageLoader.displayImage(temp[2], img);
		// itemlist.addView(v);
		// }
		// // 一张大图
		// app().imageLoader.displayImage(bean.thumb, bottom_img);
		// // 最底下的文字
		// bottom_info.setText("" + bean.readme);

	}

	/**
	 * 按图片调整view的高度 2063 640
	 */
	public void resizeView(ImageView view, Bitmap bitmap) {
		int height = bitmap.getHeight();
		int width = bitmap.getWidth();
		int newH = (int) ((IApplication.SCREEN_WIDTH - 20 * 1.5) * height / width);
		LayoutParams ps = view.getLayoutParams();
		ps.height = newH;
		// ps.height = IApplication.SCREEN_WIDTH / 2;
		view.setLayoutParams(ps);
	}

	private Spanned enter(String str) {
		String s = "";
		for (int i = 0; i < str.length(); i++) {
			s += (str.substring(i, i + 1) + "<br>");
		}
		return Html.fromHtml(s);
	}

	/********************************************************
	 * 数据相关
	 */
	class GetInfo extends WKAsyncTaskPro {
		public GetInfo(Context context) {
			super(context, true);
		}

		@Override
		protected String doInBackground(boolean... isLocal) {
			return app().net.getProductInfo(id, isLocal);
		}

		@Override
		protected void doOnPostExecute(String result, boolean isLocal) {
			Type cvbType = new TypeToken<BKBaseBean<ProductBean>>() {
			}.getType();
			Gson gson = new Gson();
			BKBaseBean<ProductBean> bean = gson.fromJson(result, cvbType);
			if (bean.status == 1) {
				updateUI(bean.data);
				// updateUI2(bean.data);
			} else {
			}
		}
	}

	/**
	 * 添加购物车接口
	 */
	class AddProductTask extends WKAsyncTaskPro {
		public AddProductTask(Context context) {
			super(context);
		}

		@Override
		protected String doInBackground(boolean... params) {
			return app().net.postAddCart("" + id, "1");
		}

		@Override
		protected void doOnPostExecute(String result, boolean isLocal) {
			Type cvbType = new TypeToken<BKBaseBean>() {
			}.getType();
			Gson gson = new Gson();
			BKBaseBean bean = gson.fromJson(result, cvbType);
			if (bean.status == 1) {
				// IApplication.showToast(bean.info);
				// finish();
				TabAct.self.setTab(3);
			} else {
				// IApplication.showToast(bean.info);
			}

		}
	}

}
